import Vue from "vue";
import VueRouter from "vue-router";
import { loadView } from "./import";

import Login from "@/components/Login";
import Index from "@/components/Index";

Vue.use(VueRouter);

var menus = window.sessionStorage.menu; // 登录成功返回的菜单

function isEmptyObject(obj) {
  for (var key in obj) {
    return false;
  }
  return true;
}

let createMenu = ms => {
  var menuItems = [];
  if (!isEmptyObject(ms)) {
    menuItems = JSON.parse(ms);
  }
  // console.log('menuItems',menuItems);
  let r = [];
  if (menuItems.length != 0) {
    menuItems.forEach((x, i) => {
      //console.log('item', x);
      let tmp = {};
      tmp.path = x.url;
      tmp.component = loadView(x.component);
      tmp.name = x.name;
      tmp.permission = x.code;
      r.push(tmp);
    });
  }
  return r;
};

let routers = [
  {
    path: "/",
    redirect: "/index",
    meta: {
      auth: true
    }
  },
  {
    path: "/index",
    name: "index",
    component: Index,
    meta: {
      auth: true
    }
  },
  {
    path: "/login",
    name: "login",
    component: Login,
    meta: {
      auth: false
    },
    hidden: true
  },
  ...createMenu(menus)
];

//console.log(routers);

export default new VueRouter({
  routes: routers
});
